package cn.exrick.xboot.generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: xujiaxin
 * @Date: 2020-02-02 19:26
 * @Description: 代码自动生成
 */

public class Generator {
//    public static final String tableName = "share_check_base_info,share_deny_permit_info,share_permit_alter_info" +
//            ",share_permit_cancel_info,share_permit_repeal_info,share_permit_revoke_info," +
//            "share_punish_alter_info,share_punish_base_info,share_punish_file_info";


    public static final String tableName = "cum_address";



    //    public static final String dataUrl = "jdbc:mysql://47.101.146.74:3306/dzzz_plus?useUnicode=true&characterEncoding=utf-8";
    public static final String dataUrl = "jdbc:mysql://175.24.182.128:63306/shop?useUnicode=true&characterEncoding=utf-8&useSSL=false";
//    public static final String dataUrl = "jdbc:mysql://172.18.110.179:63306/bidding?useUnicode=true&characterEncoding=utf-8&useSSL=false";
//    public static final String dataUrl = "jdbc:dm://localhost:5236/X_DZYZ";
//    public static final String dataUrl = "jdbc:dm://172.18.110.188:5236/X_DZYZ";

    //    public static final String driverName = "dm.jdbc.driver.DmDriver";
    public static final String driverName = "com.mysql.jdbc.Driver";

    public static final String username = "root";

    public static final String password = "Xjx@10203040";

    public static final String parent = "cn.exrick.xboot.cummunity";

    public static final String modules = "/xboot-modules/xboot-cummunity";

    /**
     * 模版包路径
     */
    public static final String entityVm = "/temp/entity.java";
    public static final String controllerVm = "/temp/controller.java";
    public static final String mapperVm = "/temp/mapper.java";
    public static final String serviceVm = "/temp/service.java";
    public static final String serviceImplVm = "/temp/serviceImpl.java";
    public static final String mapperXmlVm = "/temp/mapper.xml";

    public static boolean isPrefix = false; //是否去掉表前缀

    public static final String prefix = ""; //需要去除的前缀


    public static void generator(){
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        final String projectPath = System.getProperty("user.dir") + modules;
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("xujiaxin");
        gc.setOpen(false);
        // gc.setSwagger2(true); 实体属性 Swagger2 注解
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(dataUrl);
        // dsc.setSchemaName("public");
        dsc.setDriverName(driverName);
        dsc.setUsername(username);
        dsc.setPassword(password);
//        dsc.setDbType(DbType.DM);
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);

        // 包配置
        final PackageConfig pc = new PackageConfig();
        pc.setModuleName(""); // 没有子模块
        pc.setParent(parent);
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };

        // 如果模板引擎是 freemarker
//        String templatePath = "/templates/mapper.xml.ftl";
        // 如果模板引擎是 velocity
        String templatePath = mapperXmlVm + ".vm";

        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<FileOutConfig>();
        // 自定义配置会被优先输出
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名 ， 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化！！
                return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
 /*
        cfg.setFileCreate(new IFileCreate() {
            @Override
            public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
                // 判断自定义文件夹是否需要创建
                checkDir("调用默认方法创建的目录");
                return false;
            }
        });
        */
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();

        // 配置自定义输出模板
        //指定自定义模板路径，注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
        templateConfig.setController(controllerVm);
//        templateConfig.setXml("/temp/mapper.xml");
        templateConfig.setEntity(entityVm);
        templateConfig.setMapper(mapperVm);
        templateConfig.setService(serviceVm);
        templateConfig.setServiceImpl(serviceImplVm);

        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//        strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        // 公共父类
//        strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
        // 写于父类中的公共字段
//        strategy.setSuperEntityColumns("id");

        strategy.setInclude(tableName.split(","));
        strategy.setControllerMappingHyphenStyle(true);
//        strategy.setTablePrefix(pc.getModuleName() + "_");
        if(isPrefix){
            strategy.setTablePrefix(prefix);
        }
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new VelocityTemplateEngine());
        mpg.execute();
    }

    public static void main(String[] args) {
        generator();
    }


}
